package com.bjpowernode.boss.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bjpowernode.boss.common.cilent.RedisClient;
import com.bjpowernode.boss.common.constants.KeyConstants;
import com.bjpowernode.boss.common.utils.CacheUtil;
import com.bjpowernode.boss.convert.PayConvert;
import com.bjpowernode.boss.db.domain.DicPayType;
import com.bjpowernode.boss.db.domain.PayProduct;
import com.bjpowernode.boss.db.domain.PayWay;
import com.bjpowernode.boss.db.mapper.DicPayTypeMapper;
import com.bjpowernode.boss.db.mapper.DicPayWayMapper;
import com.bjpowernode.boss.db.mapper.PayProductMapper;
import com.bjpowernode.boss.db.mapper.PayWayMapper;
import com.bjpowernode.boss.web.models.dto.DicPayTypeDto;
import com.bjpowernode.boss.web.models.dto.DicPayWayDto;
import com.bjpowernode.boss.web.models.dto.PayProductDto;
import com.bjpowernode.boss.web.models.vo.PayWayVo;
import com.bjpowernode.boss.web.service.PayWayService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.bjpowernode.boss.PaymentApplication.CACHEMAP;

@Service
public class PayWayServiceImpl implements PayWayService {
    @Resource
    RedisClient redisClient;
    @Resource
    PayWayMapper payWayMapper;
    @Resource
    PayProductMapper payProductMapper;
    @Resource
    DicPayWayMapper dicPayWayMapper;
    @Resource
    DicPayTypeMapper dicPayTypeMapper;
    @Resource
    PayConvert convert;
    @Override
    public List<PayWayVo> getPayWayTable() {
        List<PayWay> payWays;
        try {
            String payWayJSON = redisClient.getValue(KeyConstants.PAY_WAY_ALL);
            if(StrUtil.isNotBlank(payWayJSON)){
                payWays = JSONUtil.toList(payWayJSON, PayWay.class);
            }else{
                synchronized (this) {
                    payWayJSON = redisClient.getValue(KeyConstants.PAY_WAY_ALL);
                    if(StrUtil.isNotBlank(payWayJSON)){
                        payWays = JSONUtil.toList(payWayJSON,PayWay.class);
                    }else{
                        payWays = payWayMapper.selectList(null);
                        if(!payWays.isEmpty()){
                            redisClient.setValue(KeyConstants.PAY_WAY_ALL,JSONUtil.toJsonStr(payWays));
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            payWays = payWayMapper.selectList(null);
        }
        return convert.toPayWayVos(payWays);
    }

    @Override
    public boolean changeStatus(String id, String status) {
        String payWayJSON = redisClient.getValue(KeyConstants.PAY_WAY_ALL);
        if(StrUtil.isNotBlank(payWayJSON)){
            redisClient.deleteValue(KeyConstants.PAY_WAY_ALL);
        }
        UpdateWrapper<PayWay> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status", status).eq("id",id);
        int number = payWayMapper.update(updateWrapper);
        return number>0;
    }

    @Override
    public boolean singleDeletePayWay(String id) {
        String payWayJSON = redisClient.getValue(KeyConstants.PAY_WAY_ALL);
        if(StrUtil.isNotBlank(payWayJSON)){
            redisClient.deleteValue(KeyConstants.PAY_WAY_ALL);
        }
        int number = payWayMapper.deleteById(id);
        return number>0;
    }

    @Override
    public boolean batchDeletePayWay(List<String> ids) {
        String payWayJSON = redisClient.getValue(KeyConstants.PAY_WAY_ALL);
        if(StrUtil.isNotBlank(payWayJSON)){
            redisClient.deleteValue(KeyConstants.PAY_WAY_ALL);
        }
        int number = payWayMapper.deleteBatchIds(ids);
        return number>0;
    }

    @Override
    public synchronized Map<String, Object> getAllOptions() {
        Map<String,Object> map = new HashMap<>();
        List<PayProductDto> payProducts = CacheUtil.getCache(()->{
            return (List<PayProductDto>)CACHEMAP.get(KeyConstants.PAY_PRODUCT_OPTION);
        },()->{
            return payProductMapper.selectList();
        },(data)->{
            CACHEMAP.put(KeyConstants.PAY_PRODUCT_OPTION, data);
        });
        map.put(KeyConstants.PAY_PRODUCT_OPTION,payProducts);
        List<DicPayWayDto> payWays = CacheUtil.getCache(()->{
            return (List<DicPayWayDto>)CACHEMAP.get(KeyConstants.PAY_WAY_OPTION);
        },()->{
            return dicPayWayMapper.selectList();
        },(data)->{
            CACHEMAP.put(KeyConstants.PAY_WAY_OPTION, data);
        });
        map.put(KeyConstants.PAY_WAY_OPTION,payWays);
        List<DicPayTypeDto> payTypes = CacheUtil.getCache(()->{
            return (List<DicPayTypeDto>)CACHEMAP.get(KeyConstants.PAY_TYPE_OPTION);
        },()->{
            return dicPayTypeMapper.selectList();
        },(data)->{
            CACHEMAP.put(KeyConstants.PAY_TYPE_OPTION, data);
        });
        map.put(KeyConstants.PAY_TYPE_OPTION,payTypes);
        return map;
    }
}
